package com.eclipse.training.extensions;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public class LogManager {
	private static LogManager logger = null;

	protected LogManager() {
	}

	private void log(int severity, int code, String message, Throwable throwable) {
		ILog eclipseLogger = Activator.getDefault().getLog();
		
		IStatus logElement = createStatus(severity, code, message, throwable);
		
		eclipseLogger.log(logElement);
	}

	private IStatus createStatus(int severity, int code, String message,
			Throwable throwable) {
		return new Status(severity, Activator.PLUGIN_ID, code, message,
				throwable);
	}

	public static LogManager getLogger() {
		if (logger == null)
			logger = new LogManager();
		return logger;
	}

	public void logInfo(String message) {
		log(IStatus.INFO, IStatus.OK, message, null);
	}

	public void logError(Throwable throwable) {
		String message = throwable.getMessage();
		if (message != null)
			log(IStatus.ERROR, IStatus.OK, message, throwable);
		else
			log(IStatus.ERROR, IStatus.OK, "Unexpected Exception", throwable); //$NON-NLS-1$
	}

	public void logError(String message, Throwable throwable) {
		log(IStatus.ERROR, IStatus.OK, message, throwable);
	}

	public void logError(String message) {
		log(IStatus.ERROR, IStatus.OK, message, null);
	}

	public void logWarning(String message) {
		log(IStatus.WARNING, IStatus.OK, message, null);
	}
}
